/*
 * Copyright (C) 2009 Rafael Fernandes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.android.activities.main;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ExpandableListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;

import com.phonebooksharing.android.R;
import com.phonebooksharing.android.activities.contacts.ContactBrowserByGroupActivity;
import com.phonebooksharing.android.activities.image.ImageGallerySharingContainer;
import com.phonebooksharing.android.activities.msg.MessageGroupSelection;
import com.phonebooksharing.android.activities.privacy.PrivacyGroupSetupListActivity;
import com.phonebooksharing.android.activities.video.VideoGallerySharingContainer;
import com.phonebooksharing.android.activities.wishlist.WishlistSharingContainer;
import com.phonebooksharing.android.util.GeneralUtils;
import com.phonebooksharing.android.widgets.adapters.GenericExpandableListAdapter;
import com.phonebooksharing.android.widgets.header.ListWithHeaderHelper;

public class ShareActivity extends ExpandableListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.list_layout_expand_with_empty);

        getWindow().setBackgroundDrawable(null);
        
        ExpandableListView e = getExpandableListView();
        // allow focus inside of rows to select children
        e.setItemsCanFocus(true);
        e.setCacheColorHint(0);
        e.setDivider(getResources().getDrawable(R.drawable.black_bar));
        
        HashMap<String, List<Map<String, ?>>> map = new LinkedHashMap<String, List<Map<String,?>>>();
        addShared(map);
        addSharing(map);
        addShareMng(map);
//        addFeatures(map);
        
        setListAdapter(new GenericExpandableListAdapter(this, map));
        //expand always the first
        e.expandGroup(0);
    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        if(groupPosition == 0) {//browse by
            switch (childPosition) {
                case 0: {
                    startActivity(new Intent(this, ContactBrowserByGroupActivity.class));
                    break;
                }
                case 1: {
                    Intent i = new Intent(this, ContactBrowserByGroupActivity.class);
                    i.putExtra(ContactBrowserByGroupActivity.IS_FAVORITE, true);
                    startActivity(i);
                    break;
                }
                default:
                    break;
            }
        } else if(groupPosition == 1) {//Create New
            switch (childPosition) {
                case 0: {//shout
                    GeneralUtils.postTextMessageDialog(ShareActivity.this, "Shouting at ", null);
                    break;
                }
                case 1: {//message
                    showDialog(0);
                    break;
                }
                case 2: {//postcard
                    Intent i = new Intent(ShareActivity.this, MessageGroupSelection.class);
                    i.putExtra(MessageGroupSelection.IS_POST_CARD, true);
                    startActivity(i);
                    break;
                }
            }
        } else if(groupPosition == 2) {//Manage my shareable items
            switch (childPosition) {
                case 0: {
                    startActivity(new Intent(this, PrivacyGroupSetupListActivity.class));
                    break;
                }
                case 1: {
                    startActivity(new Intent(this, ImageGallerySharingContainer.class));
                    break;
                }
                case 2: {
                    startActivity(new Intent(this, VideoGallerySharingContainer.class));
                    break;
                }
                case 3: {
                    startActivity(new Intent(this, WishlistSharingContainer.class));
                    break;
                }
            }
        }
        
        return super.onChildClick(parent, v, groupPosition, childPosition, id);
    }
    
    @Override
    protected Dialog onCreateDialog(int id) {
        return new AlertDialog.Builder(this)
            .setTitle("Message Kind")
            .setItems(R.array.message_kind, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    boolean isVoiceMessage = (which == 1);

                    Intent i = new Intent(ShareActivity.this, MessageGroupSelection.class);
                    i.putExtra(MessageGroupSelection.IS_VOICE_MSG, isVoiceMessage);
                    startActivity(i);
                }
            })
            .create();
    }
    
    protected void addShared(HashMap<String, List<Map<String, ?>>> a) {
        ArrayList<Map<String, ?>> l = new ArrayList<Map<String, ?>>();
        l.add(ListWithHeaderHelper.createEntry("Friends X Groups", "Navigate on their shareable space", R.drawable.friends));
        l.add(ListWithHeaderHelper.createEntry("Favorites", "Navigate on their shareable space", R.drawable.favorites));
//        l.add(ListWithHeaderHelper.createEntry("Most Contacted", "Navigate on their shareable space", R.drawable.group));
        
        a.put("Browse by", l);
    }
    
    protected void addSharing(HashMap<String, List<Map<String, ?>>> a) {
        ArrayList<Map<String, ?>> l = new ArrayList<Map<String, ?>>();
        l.add(ListWithHeaderHelper.createEntry("Shout", "Shout to everybody", R.drawable.dash_shout));
        l.add(ListWithHeaderHelper.createEntry("Message", "Send messages to a group or friend", R.drawable.dash_messages));
        l.add(ListWithHeaderHelper.createEntry("Post Card", "Create and share post cards", R.drawable.dash_post_card));
//        l.add(ListWithHeaderHelper.createEntry("Wish List Item", "Add new items to your wish list", R.drawable.gift));
//        l.add(ListWithHeaderHelper.createEntry("Recommendation", "Manage your recommended places", R.drawable.dash_places_nearby));
        
        a.put("Create new", l);
    }
    
    protected void addShareMng(HashMap<String, List<Map<String, ?>>> a) {
        ArrayList<Map<String, ?>> l = new ArrayList<Map<String, ?>>();
        l.add(ListWithHeaderHelper.createEntry("Groups",        "(xxx)","Manage your shareable groups", R.drawable.friends));
//        l.add(ListWithHeaderHelper.createEntry("My Location",   "Manage your location", R.drawable.my_location));
        l.add(ListWithHeaderHelper.createEntry("Image Gallery", "(xxx)","Manage your shareable image gallery", R.drawable.dash_photos));
        l.add(ListWithHeaderHelper.createEntry("Video Gallery", "(xxx)","Manage your shareable video gallery", R.drawable.dash_videos));
//        l.add(ListWithHeaderHelper.createEntry("Post Cards", "(2)", "Manage your post cards", R.drawable.dash_post_card));
        l.add(ListWithHeaderHelper.createEntry("Wish List", "(xxx)", "Manage your wish list", R.drawable.gift));
        
        a.put("Manage my shareable items", l);
    }
    
    protected void addFeatures(HashMap<String, List<Map<String, ?>>> a) {
        ArrayList<Map<String, ?>> l = new ArrayList<Map<String, ?>>();
        l.add(ListWithHeaderHelper.createEntry("Recommendation", "Locate business nearby you", R.drawable.dash_places_nearby));
        
        a.put("Extra Features", l);
    }
}
